from app import db
from datetime import datetime

from app.models import Product, Shop, User


class Order(db.Model):
    __tablename__ = 'order'

    order_id = db.Column(db.String(36), primary_key=True)
    order_no = db.Column(db.String(32), unique=True, nullable=False)
    user_id = db.Column(db.String(36),db.ForeignKey('sys_user.ID') ,nullable=False)
    shop_id = db.Column(db.String(36), db.ForeignKey('shop.shop_id'), nullable=False)
    product_id = db.Column(db.String(36), db.ForeignKey('product.product_id'), nullable=False)
    total_amount = db.Column(db.Numeric(10, 2), nullable=False)
    payment_method = db.Column(db.String(20))
    payment_status = db.Column(db.String(20), default='0')  # 0:未支付 1:已支付 2:已退款
    order_status = db.Column(db.String(20), default='0')  # 0:待支付 1:已支付 2:已完成 3:已取消
    remark = db.Column(db.String(255))
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)


    def __init__(self, order_id, order_no, user_id, shop_id, product_id, total_amount,
                 payment_method=None, payment_status='0', order_status='0', remark=None):
        self.order_id = order_id
        self.order_no = order_no
        self.user_id = user_id
        self.shop_id = shop_id
        self.product_id = product_id
        self.total_amount = total_amount
        self.payment_method = payment_method
        self.payment_status = payment_status
        self.order_status = order_status
        self.remark = remark

    # 定义关联关系
    product = db.relationship('Product', backref=db.backref('orders', lazy='dynamic'))
    shop = db.relationship('Shop', backref=db.backref('orders', lazy='dynamic'))
    user = db.relationship('User', backref=db.backref('orders', lazy='dynamic'))

    def to_dict(self):
        data = {
            'orderId': self.order_id,
            'orderNo': self.order_no,
            'userId': self.user_id,
            'shopId': self.shop_id,
            'productId': self.product_id,
            'totalAmount': float(self.total_amount) if self.total_amount else None,
            'paymentMethod': self.payment_method,
            'paymentStatus': self.payment_status,
            'orderStatus': self.order_status,
            'remark': self.remark,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'updateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }
        
        # 添加商品信息
        if self.product:
            data.update({
                'productName': self.product.product_name,
                'productPrice': float(self.product.price) if self.product.price else None,
                'productDesc': self.product.product_desc,
                'coverImg': self.product.cover_img
            })
        # user
        if self.user:
            data.update({
                'userName': self.user.LOGINNAME,
                'userPhone': self.user.PHONENUMBER
            })
            
        return data